#include "CustomTableDelegate.h"



CustomTableDelegate::CustomTableDelegate(QObject *parent)
: QStyledItemDelegate(parent) {

}

CustomTableDelegate::~CustomTableDelegate()
{

}

void CustomTableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);

    QBrush backgroundColor = index.column() % 2 == 0 ? QColor("#414B59") : QColor("#515A65");

    //绘制背景颜色（仅对当前单元格）
    painter->fillRect(option.rect, backgroundColor);

    //给选中行添加背景图片
    if (option.state & QStyle::State_Selected) {
        QPixmap backgroundImage(":/images/icons/E-panel-select-32.png");

        // 获取整行的区域
        QRect rowRect(0, option.rect.y(), option.widget->width(), option.rect.height());
        painter->drawPixmap(rowRect, backgroundImage);
    }

    //绘制空白区域的每一列的背景色
    if (index.row() == (index.model()->rowCount() - 1)) {
        QRect blankArea(option.rect.left(), option.rect.bottom(), option.widget->width(), option.widget->height());
        painter->fillRect(blankArea, backgroundColor);
    }

    // 设置文本颜色
    if (option.state & QStyle::State_Selected) {
        opt.palette.setColor(QPalette::Text, Qt::white);
    } else {
        opt.palette.setColor(QPalette::Text, QColor("#9198A3"));
    }

    // 设置字体和其他样式
    opt.displayAlignment = Qt::AlignCenter;
    opt.font.setFamily("Arial");
    opt.font.setPointSize(10);

    // 调用基类的 paint 方法来绘制文本和其他内容
    QStyledItemDelegate::paint(painter, opt, index);

}
